﻿(function(A)
{
    A.extend(A.fn,
    {
        pstrength: function(B)
        {
            var B = A.extend(
            {
                verdects: ["弱", "中等", "强"],
                colors: ["#f00", "#f60", "#3f0"],
                scores: [0, 1, 2],
                minchar: 6
            }, B);
            return this.each
            (
                function()
                {
                    var C = A(this).attr("id");
                    A(this).after("<div class=\"pstrength-info\" id=\"" + C + "_text\"></div>");
                    A(this).after("<div class=\"pstrength-bar\" id=\"" + C + "_bar\" style=\"border: 1px solid white; font-size: 1px; height: 5px; width: 0px;\"></div>");
                    A(this).keyup(
                    function()
                    {
                        A.fn.runPassword(A(this).val(), C, B)
                    })
                }
            )
        },
        runPassword: function(D, F, C)
        {
            nPerc = A.fn.checkPassword(D, C);
            var B = "#" + F + "_bar"; var E = "#" + F + "_text";

            if (nPerc == C.scores[0])
            {
                strColor = C.colors[0];
                strText = C.verdects[0];
                A(B).css({ width: "15%" })
            }
            else
            {
                if (nPerc == C.scores[1])
                {
                    strColor = C.colors[1];
                    strText = C.verdects[1];
                    A(B).css({ width: "30%" })
                }
                else
                {
                    strColor = C.colors[2];
                    strText = C.verdects[2];
                    A(B).css({ width: "45%" })
                }
            }
            A(B).css({ backgroundColor: strColor });
            A(E).html("<span style='color: " + strColor + ";'>" + strText + "</span>")
        },
        checkPassword: function(C, B)
        {
            var F = -1;
            var E = B.verdects[0];
            if (C.length < B.minchar)
            {
                F = 0;
            }
            else
            {
                if (C.match(/[a-z]/g))
                {
                    F++;
                }
                if (C.match(/[A-Z]/g))
                {
                    F++;
                }
                if (C.match(/[0-9]/g))
                {
                    F++;
                }
                if (C.match(/(.[^a-z0-9])/ig))
                {

                    F++;
                }
                if (C.length < 6 && F > 0)
                {
                    F--;
                }
            }
            return F
        }
    })
})(jQuery)


//弱密码验证
function ValidateWeakPwd(src, args)
{
    args.IsValid = (args);
}

//中强度密码验证
function ValidateMediumPwd(src, args)
{
    var password = args.Value;
    var intensityLevel = -1;
    intensityLevel = ValidateStrength(password);
    args.IsValid = (intensityLevel >= 1 || password == "");
}


//高强度密码验证
function ValidateStrongPwd(src, args)
{
    var password = args.Value;
    var intensityLevel = -1;
    intensityLevel = ValidateStrength(password);
    args.IsValid = (intensityLevel >= 2 || password == "");
}

//验证
function ValidateStrength(s)
{
    if (s.length == 0)
    {
        return -1;
    }
    if (s.length < this.minLength)
    {
        return 0;
    }

    //初始化
    var intensityLevel = -1;

    if (s.match(/[a-z]/g))
    {
        intensityLevel++;
    }
    if (s.match(/[A-Z]/g))
    {
        intensityLevel++;
    }
    if (s.match(/[0-9]/g))
    {
        intensityLevel++;
    }
    if (s.match(/(.[^a-z0-9])/ig))
    {
        intensityLevel++;
    }
    if (s.length < 6 && intensityLevel > 0)
    {
        intensityLevel--;
    }
    return intensityLevel;
}